﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using DevExpress.XtraBars.Navigation;
using DevExpress.XtraEditors;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client.frms;

namespace ECare.Client
{
    public partial class MainForm : XtraForm
    {
        public MainForm()
        {
            InitializeComponent();

            Load += MainForm_Load;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            //load the items.
            //    this.menuStrip1.Items.Clear();       
            tileNavPane1.Categories.Clear();
            var frmLogin = new frmLogin();
            if (frmLogin.ShowDialog() == DialogResult.Cancel)
            {

                try
                {
                    Close();
                }
                catch (Exception ex)
                {
 
                }
             //   return;
            
            
            }
            else
            {
                lblCrrentUser.Text = string.Format("当前登录人(Current User): {0}", ApplicationData.CurrentUserInfo.UserLoginName);
                ApplicationData.Permissions = RoleInfoHelper.GetRoleById(ApplicationData.CurrentUserInfo.RoleId).Permissions;
                LoadButtons();
                var frmHome = new frmHome();
                frmHome.MdiParent = this;
                frmHome.Show();
            }
        }

        private void LoadButtons()
        {
            tileNavPane1.Categories.Clear();
            List<ModuleInfo> items = ModulesHelper.GetMenuItems();
            IEnumerable<ModuleInfo> topitems = items.Where(a => a.ModuleParentId == -1);


            foreach (ModuleInfo item in topitems)
            {
                if (!ApplicationData.Permissions.Modules.Contains(item.ModuleId))
                    continue;


                var newitem = new TileNavCategory();
                newitem.Caption = item.ModuleName;
                newitem.Tag = item.frmType;
                newitem.OptionsDropDown.BackColor = Color.Empty;

                var title = new TileItemElement();
                title.Text = item.ModuleName;

                newitem.Tile.Elements.Add(title);
                newitem.Tile.ItemSize = TileBarItemSize.Default;

                newitem.Name = item.ModuleName;
                newitem.OwnerCollection = tileNavPane1.Categories;
                tileNavPane1.Categories.Add(newitem);


                //load the sub items....
                foreach (ModuleInfo child in item.Children)
                {
                    if (!ApplicationData.Permissions.Modules.Contains(child.ModuleId))
                        continue;

                    var newchild = new TileNavItem
                    {
                        Caption = child.ModuleName,
                        Name = child.ModuleName,
                        Tag = child.frmType
                    };
                    newchild.OptionsDropDown.BackColor = Color.Empty;
                    newchild.OwnerCollection = newitem.Items;

                    newchild.ElementClick += newchild_ElementClick;


                    var childtitle = new TileItemElement();
                    childtitle.Text = child.ModuleName;
                    newchild.Tile.Elements.Add(childtitle);
                    newchild.Tile.ItemSize = TileBarItemSize.Default;

                    newitem.Items.Add(newchild);
                }
            }
        }

        private void newchild_ElementClick(object sender, NavElementEventArgs e)
        {
            var item = sender as TileNavItem;
            string currentfrm = item.Tag.ToString();
            if (string.IsNullOrEmpty(currentfrm))
                return;

            //  if(this.OwnedForms.Where(a=>a.GetType().Equals(item.Tag.ToString() ) ))

            Form frm = MdiChildren.FirstOrDefault(a => a.GetType().FullName.Equals(currentfrm));
            if (frm != null)
            {
                frm.Activate();
                return;
            }

            Assembly asm = Assembly.GetExecutingAssembly();
            frm = asm.CreateInstance(item.Tag.ToString()) as Form;
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void itemResources_Click(object sender, EventArgs e)
        {
            //物料信息
            var frm = new frmResourcesSupply();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void navButton3_ElementClick(object sender, NavElementEventArgs e)
        {
            var frm = new frmResourcesSupply();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        #region LoadMenus

        private void newItem_Click(object sender, EventArgs e)
        {
            var item = sender as ToolStripMenuItem;
            string currentfrm = item.Tag.ToString();
            if (string.IsNullOrEmpty(currentfrm))
                return;

            //  if(this.OwnedForms.Where(a=>a.GetType().Equals(item.Tag.ToString() ) ))

            Form frm = MdiChildren.FirstOrDefault(a => a.GetType().FullName.Equals(currentfrm));
            if (frm != null)
            {
                frm.Activate();
                return;
            }

            Assembly asm = Assembly.GetExecutingAssembly();
            frm = asm.CreateInstance(item.Tag.ToString()) as Form;
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void LoadChildren(ModuleInfo currentModule, ToolStripMenuItem currentItem)
        {
            if (currentModule.Children.Count == 0)
                return;
            foreach (ModuleInfo item in currentModule.Children)
            {
                var newItem = new ToolStripMenuItem {Text = item.ModuleName, Tag = item.frmType};
                newItem.Click += newItem_Click;
                currentItem.DropDownItems.Add(newItem);

                LoadChildren(item, newItem);
            }
        }

        #endregion
    }
}